package com.basic.business.kanban.controller;

import com.basic.business.goodsIndicators.service.ITGoodsIndicatorsService;
import com.basic.business.kanban.vo.*;
import com.basic.business.unsalableBacklog.service.ITUnsalableBacklogService;
import com.basic.business.unsalablePoint.service.ITUnsalableActionPointService;
import com.basic.common.annotation.FileExport;
import com.basic.common.core.controller.BaseController;
import com.basic.common.core.domain.AjaxResult;
import com.basic.common.core.page.TableDataInfo;
import com.basic.common.utils.easyExcel.EasyExcelUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Optional;

/**
 * 滞销管理看板
 */
@RestController
@RequestMapping("/business/unsalableBoard")
public class UnsalableBoardController extends BaseController {

    @Resource
    private ITGoodsIndicatorsService tGoodsIndicatorsService;

    @Resource
    private ITUnsalableActionPointService tUnsalableActionPointService;

    @Resource
    private ITUnsalableBacklogService tUnsalableBacklogService;

    /**
     * 获取滞销管理数据
     *
     * @param unsalablePageReqVo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:unsalableBoard:list')")
    @GetMapping("/list")
    public TableDataInfo listLinkManage(UnsalablePageReqVo unsalablePageReqVo) {
        startPage();

        List<UnsalableRespVo> list = tGoodsIndicatorsService.getUnsalableData(unsalablePageReqVo);

        if (unsalablePageReqVo.getGoodsVisitorNumber() != null && unsalablePageReqVo.getCollectTreasureBuyRate() != null) {
            for (UnsalableRespVo item : list) {
                //匹配行动点
                Optional.ofNullable(tUnsalableActionPointService.getActionPoint(item.getGoodsVisitorNumberLevel(), item.getCollectTreasureBuyRateLevel())).ifPresent(actionPoint -> {
                    item.setJudgeType(actionPoint.getJudgeType());
                    item.setActionPoint(actionPoint.getActionPoint());
                });

                //获取近30天商品生产的待办数量
                int backLogCount = tUnsalableBacklogService.getBackLogCountByGoodsId(item.getGoodsId());
                item.setBackLogCount(backLogCount);
            }
        }
        return getDataTable(list);

    }

    /**
     * 获取滞销待办复盘数据
     *
     * @param unsalableReplayPageReqVo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:unsalableReplay:list')")
    @GetMapping("/unsalableReplayData")
    public TableDataInfo unsalableReplayData(UnsalableReplayPageReqVo unsalableReplayPageReqVo) {
        startPage();

        //已优化记录
        List<UnsalableReplayRespVo> optimizeList = tUnsalableBacklogService.getOptimizeList(unsalableReplayPageReqVo);

        return getDataTable(optimizeList);
    }

    /**
     * 获取滞销复盘汇总数据
     * @param unsalableReplayPageReqVo
     * @return
     */
    @GetMapping("/getReplayCollectData")
    @PreAuthorize("@ss.hasPermi('business:unsalableReplay:collectData')")
    public AjaxResult getReplayCollectData(UnsalableReplayPageReqVo unsalableReplayPageReqVo) {
        UnsalableCollectRespVo list = tUnsalableBacklogService.getUnsalableCollectData(unsalableReplayPageReqVo);

        return success(list);
    }

    @PreAuthorize("@ss.hasPermi('business:unsalableReplay:export')")
    @FileExport(fileName = "滞销复盘数据.xlsx")
    @PostMapping("/exportUnsalableReplayData")
    public String exportLinkReplayData(@RequestBody UnsalableReplayPageReqVo unsalableReplayPageReqVo) throws IOException {

        List<UnsalableReplayRespVo> list = tUnsalableBacklogService.getOptimizeList(unsalableReplayPageReqVo);

        return EasyExcelUtils.excelExport(UnsalableReplayRespVo.class,"滞销复盘数据", list);
    }
}
